using MediatR;
using SumerCoreDevOps.Application.Roles.Commands;
using SumerCoreDevOps.Domain.Entities;
using SumerCoreDevOps.Infrastructure.Identity;

namespace SumerCoreDevOps.Application.Roles.Handlers;

public class CreateRoleCommandHandler : IRequestHandler<CreateRoleCommand, Role>
{
    private readonly RoleManager _roleManager;

    public CreateRoleCommandHandler(RoleManager roleManager)
    {
        _roleManager = roleManager;
    }

    public async Task<Role> Handle(CreateRoleCommand request, CancellationToken cancellationToken)
    {
        return await _roleManager.CreateRoleAsync(
            request.RoleName,
            request.Description,
            request.CreatedBy
        );
    }
}

public class AssignRoleToUserCommandHandler : IRequestHandler<AssignRoleToUserCommand>
{
    private readonly RoleManager _roleManager;

    public AssignRoleToUserCommandHandler(RoleManager roleManager)
    {
        _roleManager = roleManager;
    }

    public async Task Handle(AssignRoleToUserCommand request, CancellationToken cancellationToken)
    {
        await _roleManager.AssignRoleToUserAsync(
            request.UserId,
            request.RoleId,
            request.AssignedBy
        );
    }
}

public class RevokeRoleFromUserCommandHandler : IRequestHandler<RevokeRoleFromUserCommand>
{
    private readonly RoleManager _roleManager;

    public RevokeRoleFromUserCommandHandler(RoleManager roleManager)
    {
        _roleManager = roleManager;
    }

    public async Task Handle(RevokeRoleFromUserCommand request, CancellationToken cancellationToken)
    {
        await _roleManager.RevokeRoleFromUserAsync(request.UserId, request.RoleId);
    }
}

public class AssignPermissionsToRoleCommandHandler : IRequestHandler<AssignPermissionsToRoleCommand>
{
    private readonly RoleManager _roleManager;

    public AssignPermissionsToRoleCommandHandler(RoleManager roleManager)
    {
        _roleManager = roleManager;
    }

    public async Task Handle(AssignPermissionsToRoleCommand request, CancellationToken cancellationToken)
    {
        await _roleManager.AssignPermissionsToRoleAsync(
            request.RoleId,
            request.PermissionIds,
            request.AssignedBy
        );
    }
}

public class RevokePermissionFromRoleCommandHandler : IRequestHandler<RevokePermissionFromRoleCommand>
{
    private readonly RoleManager _roleManager;

    public RevokePermissionFromRoleCommandHandler(RoleManager roleManager)
    {
        _roleManager = roleManager;
    }

    public async Task Handle(RevokePermissionFromRoleCommand request, CancellationToken cancellationToken)
    {
        await _roleManager.RevokePermissionFromRoleAsync(request.RoleId, request.PermissionId);
    }
}

public class DeleteRoleCommandHandler : IRequestHandler<DeleteRoleCommand>
{
    private readonly RoleManager _roleManager;

    public DeleteRoleCommandHandler(RoleManager roleManager)
    {
        _roleManager = roleManager;
    }

    public async Task Handle(DeleteRoleCommand request, CancellationToken cancellationToken)
    {
        await _roleManager.DeleteRoleAsync(request.RoleId);
    }
}
